package com.buildings.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.base.BaseFormActivity;
import com.buildings.bean.AddressBean;
import com.buildings.bean.ImageURL;
import com.buildings.bean.LoginBean;
import com.buildings.bean.TownHouseBean;
import com.buildings.bean.TownHouseResultBean;
import com.buildings.bean.TownNonHouse;
import com.buildings.bean.TownNonHouseResultBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DataUtils;
import com.buildings.util.DialogUtils;
import com.buildings.util.FormDataUtil;
import com.buildings.util.PickViewUtil;
import com.buildings.util.SPUtil;
import com.buildings.util.Utils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 城镇非住宅表单
 */
public class TownNonHouseFormActivity extends BaseFormActivity {

    public static final  String TOWN_NON_HOUSE = "townNonHouse";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_right_text)
    TextView tvSave;
    @BindView(R.id.tv_left_text)
    TextView tvClear;
    @BindView(R.id.et_building_number)
    EditText etNumber;//建筑物编号
    @BindView(R.id.et_lng)
    EditText etLng;//经度
    @BindView(R.id.et_lat)
    EditText etLat;//纬度
    @BindView(R.id.et_building_type)
    EditText etBuildingType;//被调查建筑物分类
    @BindView(R.id.et_province)
    EditText etProvince;//省市区
    @BindView(R.id.et_city)
    EditText etCity;//省市区
    @BindView(R.id.et_area)
    EditText etArea;//省市区
    @BindView(R.id.et_building_street)
    EditText etBuidingStreet;//路，街名
    @BindView(R.id.et_jianzhumenpaihao)
    EditText etJianZhuMenPai;//建筑门牌号
    @BindView(R.id.et_danweimingcheng)
    EditText etDanWeiMingCheng;//单位名称
    @BindView(R.id.et_building_name)
    EditText etBuildingName;//建筑名称
    @BindView(R.id.et_building_address)
    EditText etBuildingAddress;//建筑地址
    @BindView(R.id.et_chanquandanwei)
    EditText etChanQuanDanWei;//产权单位
    @BindView(R.id.et_jiegouleixing)
    EditText etJieGouLeiXing;//结构类型
    @BindView(R.id.et_jiegouleixing_qita)
    EditText getEtJieGouLeiXingQiTa;//结构类型其他
    @BindView(R.id.et_jianzhumianji)
    EditText etJianZhuMianJi;//建筑面积
    @BindView(R.id.et_dishangcengshu)
    EditText etDiShangCengShu;//建筑地上层数
    @BindView(R.id.et_dixiacengshu)
    EditText etDiXiaCengShu;//建筑地下层数
    @BindView(R.id.et_jianzhugaodu)
    EditText etJianZhuGaoDu;//建筑高度
    @BindView(R.id.et_jianzaoshijian)
    EditText etJianZaoShiJian;//建造时间
    @BindView(R.id.et_fangwuyongtu)
    EditText etFangWuYongTu;//房屋用途
    @BindView(R.id.et_fangwuyongtu_qita)
    EditText etFangWuYongTuQiTa;//房屋用途其他
    @BindView(R.id.et_yuanshefangleixing)
    EditText etYuanSheFangLeiBie;//原设防类别
    @BindView(R.id.et_yuanshefangliedu)
    EditText etYuanSheFangLieDu;//原设防烈度
    @BindView(R.id.et_gaizao)
    EditText etGaiZaoShiJian;//改造时间
    @BindView(R.id.et_kangzhenjiagu)
    EditText etKangZhenJiaGu;//抗震加固时间
    @BindView(R.id.et_xianshefangleibie)
    EditText etXianSheFangLeiBie;//现设防类别
    @BindView(R.id.et_xianshefangliedu)
    EditText etXianSheFangLieDu;//现设防烈度
    @BindView(R.id.et_liefengbianxing)
    EditText etLieFengBianXing;//裂缝变形倾斜
    @BindView(R.id.rg_jielun)
    RadioGroup rgJieLun;//初步结论

    @BindView(R.id.iv_image)
    ImageView ivImage;//图片
    @BindView(R.id.iv_image_delete)
    ImageView ivImageDelete;
    @BindView(R.id.gv_pic)
    GridView gvPic;

    @BindView(R.id.btn_commit)
    Button btnCommit;//提交

    @BindView(R.id.tv_reason)
    TextView tvReason;//审核未通过原因

    @BindView(R.id.ll_item)
    LinearLayout llItem;

    @BindView(R.id.ll_more)
    LinearLayout llMore;
    @BindView(R.id.ll_item_more)
    LinearLayout llItemMore;
    @BindView(R.id.ll_gone)
    LinearLayout llGone;
    @BindView(R.id.ll_item_gone)
    LinearLayout llItemGone;
    @BindView(R.id.tv_gone)
    TextView tvGone;
    @BindView(R.id.iv_gone)
    ImageView ivGone;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.iv_more)
    ImageView ivMore;

    @BindView(R.id.rb1)
    CheckBox checkBox1;
    @BindView(R.id.rb2)
    CheckBox checkBox2;
    @BindView(R.id.rb3)
    CheckBox checkBox3;
    @BindView(R.id.rb4)
    CheckBox checkBox4;
    private String result1 = "";
    private String result2 = "";
    private String result3 = "";
    private String result4 = "";

    //点击经纬度
    private String lng;
    private String lat;
    //定位经纬度
//    private String locationLng;
//    private String locationLat;
    private String taskName;


    private String formId;
    private String buildType;
    private String from;
    private boolean isEdit;//标识是否可编辑   true可编辑
    private boolean isFirst;//标识是否是第一次创建表单  true是
    private String userId;
    private String imageUrl;

    private ArrayList<AddressBean> options1Items = new ArrayList<>(); //省
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//区



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_town_non_house_new);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData(){
        userId = (String) SPUtil.get("userId","");
        lng = getIntent().getStringExtra("lng");
        lat = getIntent().getStringExtra("lat");
//        locationLng = getIntent().getStringExtra("locationLng");
//        locationLat = getIntent().getStringExtra("locationLat");
        taskId = getIntent().getStringExtra("taskId");
        taskName = getIntent().getStringExtra("taskName");
        formId = getIntent().getStringExtra("formId");
        buildType = getIntent().getStringExtra("buildType");
        from = getIntent().getStringExtra("from");

        isEdit = getIntent().getBooleanExtra("isEdit",true);
        isFirst = getIntent().getBooleanExtra("isFirst",false);

        tvSave.setText("保存");
        tvSave.setVisibility(View.VISIBLE);
        tvClear.setText("清空");
        tvClear.setVisibility(View.VISIBLE);
        if(TextUtils.equals(TaskMapActivity.FROM_TASK_MAP,from)){

            if(isFirst){
                initEditTextContent(true);//第一次创建，显示默认值
                getTianDiTuLocation(lng,lat,etBuildingAddress,etProvince,etCity,etArea,etBuildingName,etNumber,etBuidingStreet,null);
            }else {
                getFormData(buildType,formId);

                if(isEdit){
                    //点击地图标识查看表单, 而且可编辑
                }else {
                    //不可编辑
                    setEnabled();
                }
            }
        }else {
            checkBox1.setChecked(false);
            result1 = "";
            getFormData(buildType,formId);
            if(isEdit){
                //点击地图标识查看表单, 而且可编辑
            }else {
                //不可编辑
                setEnabled();
            }

        }

        updateLocation();

        etLng.setText(lng);
        etLat.setText(lat);
    }

    /**
     * 所有控件不可点击编辑
     */
    private void setEnabled() {
        tvSave.setVisibility(View.INVISIBLE);
        tvClear.setVisibility(View.INVISIBLE);
        btnCommit.setVisibility(View.INVISIBLE);

        etBuildingType.setEnabled(false);
        etProvince.setEnabled(false);
        etCity.setEnabled(false);
        etArea.setEnabled(false);
        etBuidingStreet.setEnabled(false);
        etJianZhuMenPai.setEnabled(false);
        etDanWeiMingCheng.setEnabled(false);
        etBuildingName.setEnabled(false);
        etBuildingAddress.setEnabled(false);
        etChanQuanDanWei.setEnabled(false);
        etJieGouLeiXing.setEnabled(false);
        getEtJieGouLeiXingQiTa.setEnabled(false);
        etJianZhuMianJi.setEnabled(false);
        etDiShangCengShu.setEnabled(false);
        etDiXiaCengShu.setEnabled(false);
        etJianZhuGaoDu.setEnabled(false);
        etJianZaoShiJian.setEnabled(false);
        etFangWuYongTu.setEnabled(false);
        etFangWuYongTuQiTa.setEnabled(false);
        etYuanSheFangLeiBie.setEnabled(false);
        etYuanSheFangLieDu.setEnabled(false);
        etGaiZaoShiJian.setEnabled(false);
        etKangZhenJiaGu.setEnabled(false);
        etXianSheFangLeiBie.setEnabled(false);
        etXianSheFangLieDu.setEnabled(false);
        etLieFengBianXing.setEnabled(false);
        checkBox1.setClickable(false);
        checkBox2.setClickable(false);
        checkBox3.setClickable(false);
        checkBox4.setClickable(false);
        ivImage.setEnabled(false);
        gvPic.setEnabled(false);
    }

        @Override
    public void myLocation(String locationLng, String locationLat) {
        super.myLocation(locationLng, locationLat);
        updateFoot(userId,taskId,locationLng,locationLat);
    }

    private void initView(){
        tvTitle.setText("城镇非住宅房屋调研表");
        etDiShangCengShu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(s.toString())){
                    int a = Integer.parseInt(s.toString());//地上层数
                    etJianZhuGaoDu.setText(a*3+"");
                }else {
                    etJianZhuGaoDu.setText("");
                }
            }
        });

        etJieGouLeiXing.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    getEtJieGouLeiXingQiTa.setVisibility(View.VISIBLE);
                }else {
                    getEtJieGouLeiXingQiTa.setVisibility(View.GONE);
                }

            }
        });
        etFangWuYongTu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(TextUtils.equals("其它",editable.toString())){
                    etFangWuYongTuQiTa.setVisibility(View.VISIBLE);
                }else {
                    etFangWuYongTuQiTa.setVisibility(View.GONE);
                }

                //中小学/幼儿园/医院/福利院
                if(TextUtils.equals("中小学",editable.toString()) || TextUtils.equals("幼儿园",editable.toString())
                        || TextUtils.equals("医院",editable.toString())  || TextUtils.equals("福利院",editable.toString())){
                    //底层混凝土柱-上部砖混\内部混凝土柱-外墙砖混\砖木\单跨框架\钢结构\木结构\其它_______
                    if(!TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(0)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(1)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(2)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(3)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(4)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(5)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(6))){
                        etJieGouLeiXing.setText(FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(0));
                    }

                }else {
                    //砖混\砖木\钢筋混凝土\钢结构\木结构\其它_______
                    if(!TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(0)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(1)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(2)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(3)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(4)) &&
                            !TextUtils.equals(etJieGouLeiXing.getText().toString(),FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(5))){
                        etJieGouLeiXing.setText(FormDataUtil.getTownNonHouserJieGouLeiXingTwo().get(0));
                    }
                }

            }
        });
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result1 = Constant.FORM_RESULT_01;
                }else {
                    result1 = "";
                }
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result2 = Constant.FORM_RESULT_02;
                }else {
                    result2 = "";
                }
            }
        });
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result3 = Constant.FORM_RESULT_03;
                }else {
                    result3 = "";
                }
            }
        });
        checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    result4 = Constant.FORM_RESULT_04;
                }else {
                    result4 = "";
                }
            }
        });


        initLayoutPicture(gvPic);
    }

    private void initEditTextContent(boolean isShowLast){
        //etNumber.setText(new Date().getTime()+"");
        etBuildingType.setText(FormDataUtil.getBuildingType().get(0));
        etJieGouLeiXing.setText(FormDataUtil.getTownNonHouserJieGouLeiXingOne().get(0));
        etJianZaoShiJian.setText(FormDataUtil.getJianZaoShiJian().get(0));
        etFangWuYongTu.setText(FormDataUtil.getFangWuYongTu().get(0));

        etYuanSheFangLeiBie.setText(FormDataUtil.getYuanSheFangLeiBieTwo().get(0));
        etYuanSheFangLieDu.setText(FormDataUtil.getYuanSheFangLieDu().get(0));
        etGaiZaoShiJian.setText(FormDataUtil.getGaiZaoShiJian().get(0));
        etKangZhenJiaGu.setText(FormDataUtil.getKangZhenJiaGu().get(0));

        etXianSheFangLeiBie.setText(FormDataUtil.getYuanSheFangLeiBieTwo().get(0));
        etXianSheFangLieDu.setText(FormDataUtil.getXianSheFangLieDu().get(0));
        etLieFengBianXing.setText(FormDataUtil.getLieFengBianXing().get(0));


        etBuidingStreet.setText("");
        etJianZhuMenPai.setText("");
        etDanWeiMingCheng.setText("");
        etBuildingName.setText("");
        etBuildingAddress.setText("");
        etChanQuanDanWei.setText("未知");

        etJianZhuMianJi.setText("0");
        etDiShangCengShu.setText("0");
        etDiXiaCengShu.setText("0");
        etJianZhuGaoDu.setText("0");
        etFangWuYongTuQiTa.setText("");

        checkBox1.setChecked(false);
        checkBox2.setChecked(false);
        checkBox3.setChecked(false);
        checkBox4.setChecked(false);
        result1="";
        result2="";
        result3="";
        result4="";

        imageUrl = "";
        ivImage.setImageResource(R.mipmap.ic_picture);
//        clearImage();

        if(isShowLast){
            String json = (String) SPUtil.get(TOWN_NON_HOUSE,"");
            if(!TextUtils.isEmpty(json)) {
                Gson gson = new Gson();
                TownNonHouse bean = gson.fromJson(json, TownNonHouse.class);

//            etNumber.setText(bean.getBuildingCode());
//            lng = bean.getBulidingLongitude();
//            lat = bean.getBulidingLatitude();
//            etLng.setText(bean.getBulidingLongitude());
//            etLat.setText(bean.getBulidingLatitude());
//            etBuildingType.setText(bean.getBulidingType());
//            province = bean.getBelongtoProvince();
//            city = bean.getBelongtoCity();
//            area = bean.getBelongtoArea();
//            if(!TextUtils.isEmpty(province)){
//                etProvince.setText(province);
//            }else {
//                etProvince.setText("");
//            }
//            if(!TextUtils.isEmpty(city)){
//                etCity.setText(city);
//            }else {
//                etCity.setText("");
//            }
//            if(!TextUtils.isEmpty(area)){
//                etArea.setText(area);
//            }else {
//                etArea.setText("");
//            }
                etBuidingStreet.setText(bean.getBelongtoStreet());
                etJianZhuMenPai.setText(bean.getBuildingNumber());
                etDanWeiMingCheng.setText(bean.getUnitName());
                // etBuildingAddress.setText(bean.getBuildingAddress());
                //etBuildingName.setText(bean.getBuildingName());
                etChanQuanDanWei.setText(bean.getPropertyUnit());

                //结构类型其他
                etJieGouLeiXing.setText(bean.getStructureType());
                getEtJieGouLeiXingQiTa.setText(bean.getStructureTypeOther());

                etJianZhuMianJi.setText(bean.getBuildingArea()+"");
                etDiShangCengShu.setText(bean.getBuildingUpNum()+"");
                etDiXiaCengShu.setText(bean.getBuildingDown()+"");
                etJianZhuGaoDu.setText(bean.getBuildingHight()+"");
                etJianZaoShiJian.setText(bean.getBuildingTime()+"");

                //房屋用途其他
                etFangWuYongTu.setText(bean.getHousePurpose());
                if(TextUtils.equals("其它",bean.getHousePurpose())){
                    etFangWuYongTuQiTa.setVisibility(View.VISIBLE);
                    etFangWuYongTuQiTa.setText(bean.getHousePurposeOther());
                }else {
                    etFangWuYongTuQiTa.setVisibility(View.GONE);
                }


                etYuanSheFangLeiBie.setText(bean.getOldFortificationType());
                etYuanSheFangLieDu.setText(bean.getOldFortificationIntensity());
                etGaiZaoShiJian.setText(bean.getIsReform());
                etKangZhenJiaGu.setText(bean.getIsSeismicReinforce());
                etXianSheFangLeiBie.setText(bean.getNowFortificationType());
                etXianSheFangLieDu.setText(bean.getNowFortificationIntensity());
                etLieFengBianXing.setText(bean.getIsDamage());
//            imageUrl = bean.getImage();
//            Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);


                String result = bean.getResult();
                if(!TextUtils.isEmpty(result)){
                    String[] array = result.split(",");
                    for(String str : array){
                        switch (str){
                            case Constant.FORM_RESULT_01:
                                result1 = str;
                                checkBox1.setChecked(true);
                                break;
                            case Constant.FORM_RESULT_02:
                                result2 = str;
                                checkBox2.setChecked(true);
                                break;
                            case Constant.FORM_RESULT_03:
                                result3 = str;
                                checkBox3.setChecked(true);
                                break;
                            case Constant.FORM_RESULT_04:
                                result4 = str;
                                checkBox4.setChecked(true);
                                break;
                        }
                    }
                }

            }
        }

    }


    @OnClick({R.id.iv_back,R.id.et_building_type,R.id.et_province,R.id.et_city,R.id.et_area,R.id.et_jiegouleixing,R.id.et_jianzaoshijian,R.id.et_fangwuyongtu,
                R.id.et_yuanshefangleixing,R.id.et_yuanshefangliedu,R.id.et_gaizao,R.id.et_kangzhenjiagu,R.id.et_xianshefangleibie,R.id.et_xianshefangliedu,
                R.id.et_liefengbianxing,R.id.iv_image,R.id.btn_commit,R.id.tv_right_text,R.id.tv_left_text,R.id.ll_item,R.id.iv_image_delete,R.id.ll_gone,R.id.ll_more})
    public void onClick(View v) {
        Utils.hintKb(activity);
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_left_text:
                //清空
                initEditTextContent(false);
                break;
            case R.id.ll_gone:
                //隐藏信息
                if(llItemGone.getVisibility() == View.VISIBLE){
                    llItemGone.setVisibility(View.GONE);
                    tvGone.setText("基本信息");
                    ivGone.setImageResource(R.mipmap.ic_bottom_tri);
                }else {
                    llItemGone.setVisibility(View.VISIBLE);
                    tvGone.setText("隐藏信息");
                    ivGone.setImageResource(R.mipmap.ic_top_tri);
                }
                break;
            case R.id.ll_more:
                //更多信息
                if(llItemMore.getVisibility() == View.VISIBLE){
                    llItemMore.setVisibility(View.GONE);
                    ivMore.setImageResource(R.mipmap.ic_bottom_tri);
                }else {
                    llItemMore.setVisibility(View.VISIBLE);
                    ivMore.setImageResource(R.mipmap.ic_top_tri);
                }
                break;
            case R.id.et_building_type://建筑分类
                final List<String> typeList = FormDataUtil.getBuildingType();
                PickViewUtil.showPickerViewOne(activity, "", typeList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = typeList.get(options1);
                        etBuildingType.setText(type);
                    }
                });
                break;
            case R.id.et_province:
                //省市区
                getDivisions("root",RegisterActivity.PROVINCE,"",null);
                break;
            case R.id.et_city:
                //省市区

                if(provinceBean == null){
                    CustomToast.showToast(activity,"请先选择省");
                    return;
                }
                getDivisions(provinceBean.getDivisionId(),RegisterActivity.CITY,provinceBean.getDivisionType(),provinceBean);
                break;
            case R.id.et_area:
                //省市区
                if(cityBean == null){
                    CustomToast.showToast(activity,"请先选择市");
                    return;
                }
                getDivisions(cityBean.getDivisionId(),RegisterActivity.AREA,"",null);
                break;
//            case R.id.et_building_street://街道
//                break;
            case R.id.et_jianzaoshijian://建造时间
                final List<String> jzsjList = FormDataUtil.getJianZaoShiJian();
                PickViewUtil.showPickerViewOne(activity, "", jzsjList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = jzsjList.get(options1);
                        etJianZaoShiJian.setText(type);
                    }
                });
                break;
            case R.id.et_fangwuyongtu://房屋用途
                final List<String> fwytList = FormDataUtil.getFangWuYongTu();
                PickViewUtil.showPickerViewOne(activity, "", fwytList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = fwytList.get(options1);
                        etFangWuYongTu.setText(type);
                    }
                });
                break;
            case R.id.et_jiegouleixing://结构类型
                //中小学/幼儿园/医院/福利院
                if(TextUtils.equals("中小学",etFangWuYongTu.getText().toString()) || TextUtils.equals("幼儿园",etFangWuYongTu.getText().toString())
                        || TextUtils.equals("医院",etFangWuYongTu.getText().toString())  || TextUtils.equals("福利院",etFangWuYongTu.getText().toString())){
                    final List<String> jglxList = FormDataUtil.getTownNonHouserJieGouLeiXingOne();
                    PickViewUtil.showPickerViewOne(activity, "", jglxList, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            String type = jglxList.get(options1);
                            etJieGouLeiXing.setText(type);
                        }
                    });
                }else {
                    final List<String> jglxList = FormDataUtil.getTownNonHouserJieGouLeiXingTwo();
                    PickViewUtil.showPickerViewOne(activity, "", jglxList, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            String type = jglxList.get(options1);
                            etJieGouLeiXing.setText(type);
                        }
                    });
                }

                break;
            case R.id.et_yuanshefangleixing://原设防类别

                final List<String> ysflbList = FormDataUtil.getYuanSheFangLeiBieTwo();
                PickViewUtil.showPickerViewOne(activity, "", ysflbList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = ysflbList.get(options1);
                        etYuanSheFangLeiBie.setText(type);
                        if (TextUtils.equals("未改造", etGaiZaoShiJian.getText().toString())) {
                            etXianSheFangLeiBie.setText(type);
                        }
                    }
                });
                break;
            case R.id.et_yuanshefangliedu://原设防烈度
                final List<String> ysfldList = FormDataUtil.getYuanSheFangLieDu();
                PickViewUtil.showPickerViewOne(activity, "", ysfldList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = ysfldList.get(options1);
                        etYuanSheFangLieDu.setText(type);
                        if (TextUtils.equals("未改造", etGaiZaoShiJian.getText().toString())) {
                            etXianSheFangLieDu.setText(type);
                        }
                    }
                });
                break;
            case R.id.et_gaizao://改造
                final List<String> gzsjList = FormDataUtil.getGaiZaoShiJian();
                PickViewUtil.showPickerViewOne(activity, "", gzsjList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = gzsjList.get(options1);
                        etGaiZaoShiJian.setText(type);
                        setCommonText();
                    }
                });
                break;
            case R.id.et_kangzhenjiagu://抗震加固时间
                final List<String> kzjgList = FormDataUtil.getKangZhenJiaGu();
                PickViewUtil.showPickerViewOne(activity, "", kzjgList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = kzjgList.get(options1);
                        etKangZhenJiaGu.setText(type);
                    }
                });
                break;
            case R.id.et_xianshefangleibie://现设防类别
                final List<String> xsflbList = FormDataUtil.getYuanSheFangLeiBieTwo();
                PickViewUtil.showPickerViewOne(activity, "", xsflbList, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = xsflbList.get(options1);
                        etXianSheFangLeiBie.setText(type);
                    }
                });
                break;
            case R.id.et_xianshefangliedu://现设防烈度

                final List<String> xsfld = FormDataUtil.getXianSheFangLieDu();
                PickViewUtil.showPickerViewOne(activity, "", xsfld, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = xsfld.get(options1);
                        etXianSheFangLieDu.setText(type);
                    }
                });
                break;
            case R.id.et_liefengbianxing://裂缝变形倾斜
                final List<String> lfbx = FormDataUtil.getLieFengBianXing();
                PickViewUtil.showPickerViewOne(activity, "", lfbx, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String type = lfbx.get(options1);
                        etLieFengBianXing.setText(type);
                    }
                });
                break;
            case R.id.iv_image://图片
//                if(myDialog == null){
//                    myDialog = DialogUtils.showRefreshDialog(activity);
//                }else {
//                    myDialog.show();
//                }
                selectImage();
                break;
            case R.id.iv_image_delete:
                //删除图片
                imageUrl = "";
                Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
                break;
            case R.id.btn_commit://提交
                String content = isComplete();
                if(!TextUtils.isEmpty(content)){
                    CustomToast.showToast(activity,content);
                    return;
                }
                DialogUtils.showIOSDialog(activity, "提示", "", "", "", "", null,new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        updateLocation();//上传活动轨迹
                        if(myDialog == null){
                            myDialog = DialogUtils.showRefreshDialog(activity);
                        }else {
                            myDialog.show();
                        }
                        myDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override public void onDismiss(DialogInterface dialog) {
                                btnCommit.setEnabled(true);
                            }
                        });

                        btnCommit.setEnabled(false);
                        //0、暂存1、提交
                        //是否填完0、否1、是
                        commit("1","1");
                    }
                },"确定要提交当前表单？");

                break;
            case R.id.tv_right_text:

                if(TextUtils.isEmpty(etNumber.getText().toString())){
                    //建筑物编号为空，不能保存操作
                    CustomToast.showToast(activity,"建筑物编号获取失败，无法保存，请重新进入该界面");
                    return;
                }
                tvSave.setEnabled(false);

                if(myDialog == null){
                    myDialog = DialogUtils.showRefreshDialog(activity);
                }else {
                    myDialog.show();
                }
                //0、暂存1、提交
                //是否填完0、否  1、是
                String str = isComplete();
                String isComplete = "0";
                if(TextUtils.isEmpty(str)){
                    //填完整的保存
                    isComplete = "1" ;
                }else {
                    isComplete = "0";
                }
                commit("0",isComplete);
                break;
        }
    }


    private String isComplete(){
        String content="";
        if(TextUtils.equals("已拆除",etBuildingType.getText().toString())){
            if(TextUtils.isEmpty(etNumber.getText().toString())){
                content="请输入建筑物编号";
                return content;
            }
            if(TextUtils.isEmpty(etLng.getText().toString())){
                content = "请输入经度";
                return content;
            }
            if(TextUtils.isEmpty(etLat.getText().toString())){
                content = "请输入纬度";
                return content;
            }
        }else {
            if(TextUtils.isEmpty(etNumber.getText().toString())){
                content="请输入建筑物编号";
                return content;
            }
            if(TextUtils.isEmpty(etLng.getText().toString())){
                content = "请输入经度";
                return content;
            }
            if(TextUtils.isEmpty(etLat.getText().toString())){
                content = "请输入纬度";
                return content;
            }
            if(TextUtils.isEmpty(etProvince.getText().toString())){
                content = "请选择省";
                return content;
            }
            if(TextUtils.isEmpty(etCity.getText().toString())){
                content = "请选择市";
                return content;
            }
            if(TextUtils.isEmpty(etArea.getText().toString())){
                content = "请选择区";
                return content;
            }
            if(TextUtils.isEmpty(etBuidingStreet.getText().toString())){
                content="请输入街道";
                return content;
            }if(TextUtils.isEmpty(etJianZhuMenPai.getText().toString())){
                content = "请输入建筑门牌号";
                return content;
            }
            if(TextUtils.isEmpty(etDanWeiMingCheng.getText().toString())){
                content = "请输入单位名称";
                return content;
            }
            if(TextUtils.isEmpty(etBuildingName.getText().toString())){
                content = "请输入建筑名称";
                return content;
            }
            if(TextUtils.isEmpty(etBuildingAddress.getText().toString())){
                content = "请输入建筑地址";
                return content;
            }
            if(TextUtils.isEmpty(etChanQuanDanWei.getText().toString())){
                content = "请输入产权单位";
                return content;
            }

            if(TextUtils.equals("其它",etJieGouLeiXing.getText().toString())){
                if(TextUtils.isEmpty(getEtJieGouLeiXingQiTa.getText().toString())){
                    content ="请输入其它结构类型的补充内容";
                    return content;
                }
            }

            if(TextUtils.isEmpty(etJianZhuMianJi.getText().toString())){
                content = "请输入建筑面积";
                return content;
            }
            if(TextUtils.isEmpty(etDiShangCengShu.getText().toString())){
                content = "请输入建筑地上层数";
                return content;
            }if(TextUtils.isEmpty(etDiXiaCengShu.getText().toString())){
                content = "请输入建筑地下层数";
                return content;
            }if(TextUtils.isEmpty(etJianZhuGaoDu.getText().toString())){
                content = "请输入建筑高度";
                return content;
            }if(TextUtils.isEmpty(etJianZaoShiJian.getText().toString())){
                content = "请选择建造时间";
                return content;
            }
            if(TextUtils.equals(etFangWuYongTu.getText(),"其它")){
                if(TextUtils.isEmpty(etFangWuYongTuQiTa.getText().toString())){
                    content ="请输入房屋用途其它补充";
                    return content;
                }
            }

            if(TextUtils.isEmpty(getResult())){
                content = "请选择初步结论";
                return content;
            }

//            if(TextUtils.isEmpty(imageUrl)){
//                content = "请选择图片";
//                return content;
//            }

        }

        return content;

    }

    private void getFormData(final String buildType, String formId) {
        String url = Api.FORM+"?type="+buildType+"&formId="+formId;
        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(json)
                .build()
                .enqueue(new BaseCallBack<TownNonHouseResultBean>() {
                    @Override
                    public void onSuccess(TownNonHouseResultBean resultBean) {
                        super.onSuccess(resultBean);
                        if(resultBean.getCode() == 200 && resultBean.getData() != null){
                            TownNonHouse bean = resultBean.getData();
                            etNumber.setText(bean.getBuildingCode());
                            buildingCode = bean.getBuildingCode();
                            lng = bean.getBulidingLongitude();
                            lat = bean.getBulidingLatitude();
                            etLng.setText(bean.getBulidingLongitude());
                            etLat.setText(bean.getBulidingLatitude());
                            etBuildingType.setText(bean.getBulidingType());
                            province = bean.getBelongtoProvince();
                            city = bean.getBelongtoCity();
                            area = bean.getBelongtoArea();
                            if(!TextUtils.isEmpty(province)){
                                etProvince.setText(province);
                            }else {
                                etProvince.setText("");
                            }
                            if(!TextUtils.isEmpty(city)){
                                etCity.setText(city);
                            }else {
                                etCity.setText("");
                            }
                            if(!TextUtils.isEmpty(area)){
                                etArea.setText(area);
                            }else {
                                etArea.setText("");
                            }
                            etBuidingStreet.setText(bean.getBelongtoStreet());
                            etJianZhuMenPai.setText(bean.getBuildingNumber());
                            etDanWeiMingCheng.setText(bean.getUnitName());
                            etBuildingAddress.setText(bean.getBuildingAddress());
                            etBuildingName.setText(bean.getBuildingName());
                            etChanQuanDanWei.setText(bean.getPropertyUnit());

                            //结构类型其他
                            etJieGouLeiXing.setText(bean.getStructureType());
                            getEtJieGouLeiXingQiTa.setText(bean.getStructureTypeOther());

                            etJianZhuMianJi.setText(bean.getBuildingArea()+"");
                            etDiShangCengShu.setText(bean.getBuildingUpNum()+"");
                            etDiXiaCengShu.setText(bean.getBuildingDown()+"");
                            etJianZhuGaoDu.setText(bean.getBuildingHight()+"");
                            etJianZaoShiJian.setText(bean.getBuildingTime()+"");

                            //房屋用途其他
                            etFangWuYongTu.setText(bean.getHousePurpose());
                            etFangWuYongTuQiTa.setText(bean.getHousePurposeOther());

                            etYuanSheFangLeiBie.setText(bean.getOldFortificationType());
                            etYuanSheFangLieDu.setText(bean.getOldFortificationIntensity());
                            etGaiZaoShiJian.setText(bean.getIsReform());
                            etKangZhenJiaGu.setText(bean.getIsSeismicReinforce());
                            etXianSheFangLeiBie.setText(bean.getNowFortificationType());
                            etXianSheFangLieDu.setText(bean.getNowFortificationIntensity());
                            etLieFengBianXing.setText(bean.getIsDamage());

                            imageUrl = bean.getImage();
//                            Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
//                            updateImage(new ArrayList<ImageURL>());
                            if(!TextUtils.isEmpty(imageUrl)){
                                String[] arrImage = imageUrl.split(",");
                                if(listUrl == null){
                                    listUrl = new ArrayList<ImageURL>();
                                }
                                for(String url : arrImage){
                                    ImageURL imageURL = new ImageURL();
                                    imageURL.setLocalPath(url);
                                    imageURL.setThumbnail(url);
                                    listUrl.add(imageURL);
                                }
                                updateImage(listUrl);
                            }

                            //是否显示审核未通过提示
                            if(!TextUtils.isEmpty(bean.getReason())){
                                tvReason.setVisibility(View.VISIBLE);
                                tvReason.setText(bean.getReason());
                            }else {
                                tvReason.setVisibility(View.GONE);
                            }


                            String result = bean.getResult();
                            if(!TextUtils.isEmpty(result)){
                                String[] array = result.split(",");
                                for(String str : array){
                                    switch (str){
                                        case Constant.FORM_RESULT_01:
                                            result1 = str;
                                            checkBox1.setChecked(true);
                                            break;
                                        case Constant.FORM_RESULT_02:
                                            result2 = str;
                                            checkBox2.setChecked(true);
                                            break;
                                        case Constant.FORM_RESULT_03:
                                            result3 = str;
                                            checkBox3.setChecked(true);
                                            break;
                                        case Constant.FORM_RESULT_04:
                                            result4 = str;
                                            checkBox4.setChecked(true);
                                            break;
                                    }
                                }
                            }

                        }else {
                            CustomToast.showToast(activity, "获取表单内容失败");
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }
                });

    }


    private void commit(String type,String isComplete) {
        String url = Api.FORM_TOWN_HOUSE+type+"/updNoCity";
        if(isFirst){
            //从地图第一次过来提交的接口
            url = Api.FORM_TOWN_HOUSE+type+"/noCityTask";
        }

        TownNonHouse bean = new TownNonHouse();
        if(!TextUtils.isEmpty(formId)){
            bean.setFwId(formId);
        }
        bean.setIsSaveAll(isComplete);//是否是全部的保存

        bean.setTaskId(taskId);
        bean.setTaskName(taskName);
        bean.setBuildingCode(etNumber.getText().toString());
        bean.setBulidingLongitude(lng);
        bean.setBulidingLatitude(lat);
        bean.setBulidingType(etBuildingType.getText().toString());

        bean.setBelongtoProvince(province);
        bean.setBelongtoCity(city);
        bean.setBelongtoArea(area);
        bean.setBelongtoStreet(etBuidingStreet.getText().toString());

        bean.setBuildingNumber(etJianZhuMenPai.getText().toString());
        bean.setUnitName(etDanWeiMingCheng.getText().toString());

        bean.setBuildingAddress(etBuildingAddress.getText().toString());
        bean.setBuildingName(etBuildingName.getText().toString());
        bean.setPropertyUnit(etChanQuanDanWei.getText().toString());

        if(TextUtils.equals("其它",etJieGouLeiXing.getText().toString())){
            bean.setStructureType(etJieGouLeiXing.getText().toString());
            bean.setStructureTypeOther(getEtJieGouLeiXingQiTa.getText().toString());
        }else {
            bean.setStructureType(etJieGouLeiXing.getText().toString());
        }



        if(TextUtils.isEmpty(etJianZhuMianJi.getText().toString())){
            etJianZhuMianJi.setText("0");
        }
        if(TextUtils.isEmpty(etDiShangCengShu.getText().toString())){
            etDiShangCengShu.setText("0");
        }
        if(TextUtils.isEmpty(etDiXiaCengShu.getText().toString())){
            etDiXiaCengShu.setText("0");
        }
        if(TextUtils.isEmpty(etJianZhuGaoDu.getText().toString())){
            etJianZhuGaoDu.setText("0");
        }
        bean.setBuildingArea(Integer.parseInt(etJianZhuMianJi.getText().toString()));
        bean.setBuildingUpNum(Integer.parseInt(etDiShangCengShu.getText().toString()));
        bean.setBuildingDown(Integer.parseInt(etDiXiaCengShu.getText().toString()));
        bean.setBuildingHight(Integer.parseInt(etJianZhuGaoDu.getText().toString()));

        bean.setBuildingTime(etJianZaoShiJian.getText().toString());

        if(TextUtils.equals("其它",etFangWuYongTu.getText().toString())){
            bean.setHousePurpose(etFangWuYongTu.getText().toString());
            bean.setHousePurposeOther(etFangWuYongTuQiTa.getText().toString());
        }else {
            bean.setHousePurpose(etFangWuYongTu.getText().toString());
        }



        bean.setOldFortificationType(etYuanSheFangLeiBie.getText().toString());
        bean.setOldFortificationIntensity(etYuanSheFangLieDu.getText().toString());
        bean.setIsReform(etGaiZaoShiJian.getText().toString());
        bean.setIsSeismicReinforce(etKangZhenJiaGu.getText().toString());//抗震加固
        bean.setNowFortificationType(etXianSheFangLeiBie.getText().toString());
        bean.setNowFortificationIntensity(etXianSheFangLieDu.getText().toString());
        bean.setIsDamage(etLieFengBianXing.getText().toString());

        imageUrl = "";
        if(listUrl != null){
            for(int i=0; i<listUrl.size(); i++){
                if(i == listUrl.size() -1){
                    imageUrl = imageUrl + listUrl.get(i).getThumbnail();
                }else {
                    imageUrl =  imageUrl + listUrl.get(i).getThumbnail() + ",";
                }
            }
        }
        bean.setImage(imageUrl);

        String result = getResult();
        bean.setResult(result);


        Gson gson = new Gson();
        String json = gson.toJson(bean);

        SPUtil.save(TOWN_NON_HOUSE,json);

        MyHttpClient
                .newBuilder()
                .post()
                .url(url)
                .addParam(json)
                .build()
                .enqueue(new BaseCallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean bean) {
                        super.onSuccess(bean);
//{"code":200,"data":{"success":true,"errorCode":null,"errorMessage":null,"retryTimes":0,"remainLockedTime":null,"userId":"ca3ca0d0d557424d94ec8009b6ca6d08","token":"eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiJhNGQ1MTkxZC01ZjY0LTQ5YjEtYTk4NS01NjM2YTA5ZDc1M2EiLCJpYXQiOjE1ODk1MzMxNDksInN1YiI6IjkzNWQ4MzNkZGU0MjRlMWJhYzdmZWVjM2M0MDZhMDI5IiwiZXhwIjoxNTg5NTMzNTQ5fQ.KGhav1E8SLuHXkb60RsbWj3xLMscIPrGmvDSmPjSLr4"},"message":null}
                        if(bean.getCode() == 200){
                            if(TextUtils.equals(TaskMapActivity.FROM_TASK_MAP,from)){
                                //地图过来的，回到任务列表
                                //startActivity(new Intent(activity,TaskListActivity.class));
                                setResult(RESULT_OK);
                                finish();
                            }else {
                                //表单列表过来的回到 表单列表  刷新列表
                                setResult(RESULT_OK);
                                finish();
                            }
                        }else {
                            tvSave.setEnabled(true);
                        }
                        if (myDialog != null)
                            myDialog.dismiss();

                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                        tvSave.setEnabled(true);

                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                        tvSave.setEnabled(true);

                    }
                });

    }


    /**
     * 最终的初步结论
     * @return
     */
    private String getResult(){
        String str = "";
        if(!TextUtils.isEmpty(result1)){
            str = str+result1+",";
        }
        if(!TextUtils.isEmpty(result2)){
            str = str+result2+",";
        }
        if(!TextUtils.isEmpty(result3)){
            str = str+result3+",";
        }
        if(!TextUtils.isEmpty(result4)){
            str = str+result4+",";
        }
        if(!TextUtils.isEmpty(str)){
            str = str.substring(0,str.length()-1);
        }
        return str;
    }

    /**
     * 调用天地图获取地理信息
     */
    private void getTianDiTuLocation(String lng,String lat) {

        String url = "http://api.tianditu.gov.cn/geocoder?postStr={'lon':"+lng+",'lat':"+lat+",'ver':1}&type=geocode&tk="+Constant.TIAN_DI_TU_ANDROID_KEY;
//        String url = "http://api.tianditu.gov.cn/geocoder?postStr={%27lon%27:116.28,%27lat%27:40.1419,%27ver%27:1}&type=geocode&tk=3bd24441a20ee975f046a8d7417b936a";
        final JsonBuilder jsonBuilder = new JsonBuilder(this);
//        jsonBuilder.put("doctorMobile", etPhone.getText().toString());

        MyHttpClient
                .newBuilder()
                .get()
                .url(url)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<String>() {
                    @Override
                    public void onSuccess(String response) {
                        super.onSuccess(response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(TextUtils.equals(jsonObject.getString("msg"),"ok")){
                                JSONObject object = jsonObject.getJSONObject("result").getJSONObject("addressComponent");
                                String formatted_address = jsonObject.getJSONObject("result").getString("formatted_address");//建筑地址
                                String provinceStr = object.getString("province");//省
                                String cityStr = object.getString("city");//市
                                String county = object.getString("county");//区
//                                String road = object.getString("road");//街道
                                String poi = object.getString("poi");//建筑名称

                                if(!TextUtils.isEmpty(formatted_address)){
                                    etBuildingAddress.setText(formatted_address);
                                }
                                if(!TextUtils.isEmpty(provinceStr)){
                                    TownNonHouseFormActivity.this.province = provinceStr;
                                    TownNonHouseFormActivity.this.city = cityStr;
                                    TownNonHouseFormActivity.this.area = county;
                                    if(!TextUtils.isEmpty(provinceStr)){
                                        etProvince.setText(provinceStr);
                                    }
                                    if(!TextUtils.isEmpty(cityStr)){
                                        etCity.setText(cityStr);
                                    }
                                    if(!TextUtils.isEmpty(county)){
                                        etArea.setText(county);
                                    }

                                }
//                                if(!TextUtils.isEmpty(road)){
//                                    etBuidingStreet.setText(road);
//                                }
                                if(!TextUtils.isEmpty(poi)){
                                    etBuildingName.setText(poi);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
//                        if (myDialog != null)
//                            myDialog.dismiss();
                    }
                });

    }

    @Override
    public void setProvinceText(String province) {
        super.setProvinceText(province);
        etProvince.setText(province);
    }

    @Override
    public void setCityText(String city) {
        super.setCityText(city);
        etCity.setText(city);
    }

    @Override
    public void setAreaText(String area) {
        super.setAreaText(area);
        etArea.setText(area);
    }

    @Override
    public void setImage(String url) {
        super.setImage(url);
//        imageUrl = url;
//        Glide.with(activity).load(imageUrl).asBitmap().error(R.mipmap.ic_picture).into(ivImage);
    }

    private void setCommonText() {
        String text = etGaiZaoShiJian.getText().toString();
        if (TextUtils.equals("未改造", text)) {
            etXianSheFangLieDu.setText(etYuanSheFangLieDu.getText().toString());
            etXianSheFangLeiBie.setText(etYuanSheFangLeiBie.getText().toString());
        }
    }

}
